package com.ueboot.weishop.service.customer.impl;

import com.ueboot.weishop.entity.customer.CustomerInvoiceEntity;
import com.ueboot.weishop.repository.customer.CustomerInvoiceRepository;
import com.ueboot.weishop.service.customer.CustomerInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Neel on 2017/9/24.
 */
@Slf4j
@Service
public class CustomerInvoiceServiceImpl implements CustomerInvoiceService {

	@Autowired
	private CustomerInvoiceRepository repository;


	@Override
	public Page<CustomerInvoiceEntity> findBy(Pageable pageable) {
		return repository.findAll(pageable);
	}

	/**
	 * 根据关键字查询
	 *
	 * @param pageable
	 * @param keyword
	 * @return
	 */
	@Override
	public Page<CustomerInvoiceEntity> findBy(Pageable pageable, String keyword) {
		CustomerInvoiceEntity entity = new CustomerInvoiceEntity();
		ExampleMatcher example = ExampleMatcher.matching()
				.withIgnoreNullValues()
				.withMatcher("name", matcher -> matcher.contains())
				.withMatcher("remark", matcher -> matcher.contains());
		return repository.findAll(Example.of(entity, example), pageable);
	}

	@Override
	public CustomerInvoiceEntity save(CustomerInvoiceEntity s) {
		return repository.save(s);
	}

	@Override
	public CustomerInvoiceEntity get(Long id) {
		return repository.findOne(id);
	}

	@Override
	public List<CustomerInvoiceEntity> findByCustomerId(Long customerId) {
		return repository.findByCustomerId(customerId);
	}

	@Override
	public void delete(Long id) {
		repository.delete(id);
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			repository.delete(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public void updateUnDefaultByCustomerId(Long customerId, Boolean unDefault) {
		repository.updateUnDefaultByCustomerId(customerId, unDefault);
	}

	@Override
	public CustomerInvoiceEntity findById(Long invoiceId) {
		return repository.findById(invoiceId);
	}

}
